using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Rocket : Weapon {
	public Vector3 destVector;
	private float Timer = 0.0f;
	private GameObject enemyTarget = null;
    private GameObject spawnerRoot;
    private bool Homing = false;
	// Use this for initialization

	
	// Update is called once per frame
    //public void SetTarget(GameObject target)
    //{
    //    enemyTarget = target;	
    //}
	void Update () 
	{
		Move();
	}
    public void HomingOn()
    {
        Homing = true;
    }
    void SetTarget()
    {
         spawnerRoot = GameObject.FindGameObjectWithTag("Spawned");
         GameObject enemy = null;
         float shootDistance = 99f;
		if(spawnerRoot.transform.childCount > 0)
		{
	        foreach (Transform child in spawnerRoot.transform)
	        {
	            //if (Vector3.Distance(rocket.transform.position, child.transform.position) < shootDistance)
	            //print("distance :" + (child.transform.position.y - rocket.transform.position.y));
	            if ((child.transform.position.y - transform.position.y) < shootDistance)
	            {
	                shootDistance = (child.transform.position.y - transform.position.y);
	                enemy = child.gameObject;
	            }
	        }
			if(enemy.tag == "Boss3")
			{
	
				List<GameObject> ShootThese;
				ShootThese = new List<GameObject>();
				foreach(Transform Child in enemy.transform.GetChild(0))
				{
					if(Child.gameObject.GetComponent<CanTakeDamage>() != null)
					{
						ShootThese.Add(Child.gameObject);
					}
				}
				enemyTarget = ShootThese[Random.Range(0, ShootThese.Count)];
//				enemyTarget = enemy.transform.GetChild(0).transform.GetChild(Random.Range(0, enemy.transform.GetChild(0).transform.childCount)).gameObject;
			}
			else{
				enemyTarget = enemy;}
		}
    }
	void Move()
	{
        //if (enemyTarget == null)
        //{
        //    SetTarget();
        //    rigidbody.MovePosition(transform.position + Vector3.up * -rocketSpeed * Time.deltaTime);
        //    //gameObject.SetActive(false);
        //}
        //rigidbody.MovePosition(transform.position +destVector * -rocketSpeed * Time.deltaTime);
        //Destroy(gameObject);

        if (Homing)
        {
            if (enemyTarget == null)
            {
                SetTarget();
            }
            else
                destVector = (transform.position - enemyTarget.transform.position);
            //			if(destVector.x > 0 && destVector.y < 0);
            //				destVector = new Vector3(destVector.x, -destVector.y,0);

            if (destVector != Vector3.zero)
            {
                if (enemyTarget == null)
                {
                    SetTarget();
                }
                else if (transform.position.x >= enemyTarget.transform.position.x)
                {
                    transform.rotation = Quaternion.Euler((Quaternion.LookRotation(enemyTarget.transform.position, Vector3.up).eulerAngles.x), -90f, -270f);
                }
                else
                {
                    transform.rotation = Quaternion.Euler(Quaternion.LookRotation(enemyTarget.transform.position, Vector3.up).eulerAngles.x, 90f, 270f);
                }

            }
        }
		rigidbody.MovePosition(transform.position +destVector.normalized * -rocketSpeed * Time.deltaTime);
		
		//rigidbody.position = Vector3.Lerp(transform.position, -destVector, rocketSpeed * Time.deltaTime);
		//rigidbody.MovePosition(Vector3.Lerp(transform.position, transform.position+destVector, rocketspeed * Time.deltaTime));
		if(!renderer.isVisible)
		{
			Destroy(gameObject);
		}
		else
		{
			Timer += Time.deltaTime;
		}		

	}
}